package com.example.android.story.ui;

import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.story.R;
import com.example.android.story.Utils.StoryViewModel;
import com.example.android.story.Utils.User;


public class SignupFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters

    private Button signin_btn;
    private TextView has_account;
    private EditText name_edit;
    private EditText password_edit;
    private EditText confirm_password_edit;

    private StoryViewModel viewModel;

//    public void setDatabase(Database database) {
//        this.database = database;
//    }

    private OnClick onClick;


    public interface OnClick{
        public void userAdded(User user);
        public void cancelSignin();
    }

    public SignupFragment() {
        // Required empty public constructor
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel= ViewModelProviders.of(getActivity()).get(StoryViewModel.class);
    }

    private void setupListener() {
        signin_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username=name_edit.getText().toString();
                String password=password_edit.getText().toString();
                String confirm=confirm_password_edit.getText().toString();
                if(!confirm.equals(password)){
                    Log.d("debug","password:"+password+" confirm: "+confirm);
                    Toast.makeText(getContext(),"两次输入的密码不匹配",Toast.LENGTH_SHORT).show();
                }
                else if(valid(username,password)){
                    User user=new User(username,password,50);
                    viewModel.insert(user);
                    onClick.userAdded(user);
                }
                else{
                    Toast.makeText(getContext(),"密码必须6位以上",Toast.LENGTH_SHORT).show();
                }
            }
        });
        has_account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClick.cancelSignin();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout_upload_story for this fragment
        return inflater.inflate(R.layout.fragment_signin, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        signin_btn=getView().findViewById(R.id.sign_in);
        has_account=getView().findViewById(R.id.has_account);
        name_edit=getView().findViewById(R.id.username);
        password_edit=getView().findViewById(R.id.password);
        confirm_password_edit=getView().findViewById(R.id.confirm_password);

        setupListener();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        onClick=(OnClick)context;

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    public boolean valid(String username, String password){
        if(username.isEmpty()==false
                &&password.isEmpty()==false)
            return true;
        return false;
    }
}
